/**
 *
 */

import java.io.*;
import java.util.Scanner;

public class FileTest {
    public static void main(String[] args) throws Exception {
        String path = "D:\\zuoye\\ceshi\\ceshi.txt";
        String fileName="D:\\zuoye";
        readFile(path);
    }
    //创建文件夹/目录
    public  static void createNewFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            System.out.println("该文件或目录已存在");
            return ;
        }
        System.out.println("是否要创建文件/目录");
        System.out.println("1创建文件    2创建目录");
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        if (i == 1) {
            try {
                file.createNewFile();
                System.out.println("成功创建文件");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
           file.mkdir();
            System.out.println("成功创建目录");
        }
        }
    //删除目录/文件
    public static boolean deleteFile(String path){
        File file = new File(path);
        if (!file.exists()) {
            System.out.println("文件不存在");
            return false;
        }
        deleteListFile(path);
        return true;
    }

    public static void deleteListFile(String path){
        File file = new File(path);
        if (file.isFile()) {
            file.delete();
            System.out.println("文件删除成功");
            return;
        }
        String[] fList = file.list();
        for (int i = 0 ; i < fList.length; i++) {
            deleteListFile(path+File.separator+fList[i]);
        }
        file.delete();
        return;
    }
    //遍历文件夹
    public static void iteratorFile(String fileName){
        File file = new File(fileName);
        String[] fileStr=file.list();
        for (int i = 0; i < fileStr.length; i++) {
            File fileChild = new File(fileName+"/"+fileStr[i]);
            if (!fileChild.isDirectory()) {
                System.out.println(fileStr[i]);
            } else {
                System.out.println(fileStr[i]+"/");
                iteratorFile(fileName+"/"+fileStr[i]);
            }
        }
    }
    //重命名文件/文件夹
    public static void renameFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            System.out.println("请输入要修改的名字");
            Scanner scan =new Scanner(System.in);
            String pathName=scan.nextLine();
            file.renameTo(new File(pathName));
            System.out.println("重命名成功");
        }
    }
    //读取/修改文件属性
    public static void readProperty(String path){
        File file = new File(path);
        System.out.println("读取文件名称"+file.getName());
        System.out.println("读取文件路径"+file.getPath());
        System.out.println("读取绝对路径"+file.getAbsolutePath());
        System.out.println("读取文件大小"+file.length()+"byte");
        System.out.println("判断文件是否被隐藏"+file.isHidden());
        System.out.println("判断文件是否可读"+file.canRead());
        System.out.println("读取文件父级路径"+new File(file.getAbsolutePath()).getParent());
        if (file.exists()) {
            //file.setReadable(true);
            //file.setWritable(true);
            //file.setReadOnly();
        }
    }
    //简单读写文件
    public static void writeFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            System.out.println("文件不存在");
        } else {
            try {
                FileWriter fw = new FileWriter(file);
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write("进去了吧");
                bw.newLine();
                bw.write("换行了吗");
                bw.flush();
                System.out.println("成功写入");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void readFile(String path) throws Exception{
        File file = new File(path);
        if (file.exists()) {
            try {
                FileReader fr=new FileReader(file);
                BufferedReader br=new BufferedReader(fr);
                String line;
                while ((line = br.readLine())!=null) {
                    System.out.println(line);
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

